package page

import (
	"errors"
	"github.com/jinzhu/gorm"
	"strconv"
)

func Pagination(db *gorm.DB, page interface{}, result interface{}) (total int, current int, err error) {
	if value, isString := page.(string); isString {
		current, _ = strconv.Atoi(value)
		db.Count(&total)
		db = db.Offset((current - 1) * 10).Limit(10)
		db.Find(result)
	} else if current, isInt := page.(int); isInt {
		db = db.Offset((current - 1) * 10).Limit(10)
		db.Count(&total)
		db.Find(result)
	} else {
		err = errors.New("error argument for page")
	}
	return
}
